// pages/addActivity/addActivity.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "",
    content: "",
    outNum: 0,
    presentList: [],
    fileList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  afterRead(event) {
    var that = this
    const {
      file
    } = event.detail;
    wx.uploadFile({
      url: 'http://127.0.0.1:8080/file/upload',
      filePath: file.url,
      name: 'file',
      formData: {},
      success(res) {
        wx.showToast({
          title: '上传成功',
        })

        // 上传完成需要更新 fileList
        const fileList = that.data.fileList;
        var url = JSON.parse(res.data).data
        fileList.push({
          ...file,
          url: url
        });
        that.setData({
          fileList
        });
      },
    });
  },

  create(){
    var that = this
    var urlList = []
    for(var item in this.data.fileList){
      console.log(this.data.fileList);
      console.log(this.data.fileList[item]);
      urlList.push({
        imageUrl:this.data.fileList[item].url
      })
    }

    console.log("结果为");
    console.log(urlList);
    this.setData({
      presentList:urlList
    })

    wx.$request(true,"/activity/edit","POST",{
      title: that.data.title,
      content: that.data.content,
      outNum: that.data.outNum,
      presentList: that.data.presentList
    })
    .then(res=>{
      wx.showToast({
        title: '创建成功',
      })

      wx.reLaunch({
        url: '/pages/index/index',
      })
    })
  }
})